package org.cst8277.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.cst8277.ums.dto.UserDto;
import org.cst8277.ums.entity.User;
import org.cst8277.ums.entity.UserRole;
import org.cst8277.ums.mapper.UserMapper;
import org.cst8277.ums.response.ResponseMessage;
import org.cst8277.ums.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author houbin
 * @since 2023-10-11
 */
@Slf4j
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public ResponseMessage login(UserDto userDto) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //这里填写的是数据库字段，不是java属性
        wrapper.eq("username", userDto.getUsername());

        int count = userMapper.selectCount(wrapper);
        if (count == 0) {
            log.error("用户不存在, userDto:{}", userDto);
            return ResponseMessage.buildFail("用户不存在");
        }
        wrapper.eq("password", userDto.getPassword());
        count = userMapper.selectCount(wrapper);
        if (count == 0) {
            log.error("密码不正确, userDto:{}", userDto);
            return ResponseMessage.buildFail("密码不正确");
        }

        return ResponseMessage.SUCCESS;
    }

    @Override
    public ResponseMessage logout(UserDto userDto) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //这里填写的是数据库字段，不是java属性
        wrapper.eq("username", userDto.getUsername());

        int count = userMapper.selectCount(wrapper);
        if (count == 0) {
            log.error("用户不存在, userDto:{}", userDto);
            return ResponseMessage.buildFail("用户不存在");
        }
        return ResponseMessage.SUCCESS;
    }
}
